package embuzzled.g5;

import java.awt.Color;
import java.awt.Point;
import java.util.ArrayList;

/**
 * MultiColorPuzzle 
 * A puzzle containing multiple colors.
 *
 */
public class MultiColorPuzzle extends SingleColorPuzzle 
{
	ArrayList<PuzzleComponent> components;
	Color[][] colorMap;
	
	public MultiColorPuzzle(int height, int width)
	{
		super(height, width);
		
		//Initialize the color map to all clear tiles
		colorMap = new Color[height][width];
		
		for(int i = 0; i < colorMap.length; i++)
			for(int j = 0; j < colorMap[0].length; j++)
				colorMap[i][j] = Constants.COLOR_CLEAR;
	}
	
	/**
	 * Attempts to add a puzzle component to this multi-color puzzle.
	 * Returns true if it was successful
	 * (that is, p could be overlaid with the current color map of this puzzle)
	 * Returns false otherwise
	 * @param p A puzzle component.
	 * @return True if p was added successfully. False otherwise.
	 */
	public boolean add(PuzzleComponent p)
	{
		//Check to make sure we're adding in a component of the same size as this puzzle
		if(p.getHeight() != getHeight() || p.getWidth() != getWidth())
			return false;
		
		Color[][] newMap = Helper.superimpose(colorMap, p.returnLocalColorMap(),new Point(0,0));
		
		if(newMap == null)
			return false;
		else
		{
			colorMap = newMap;
			components.add(p);
			return true;
		}
	}
	
	/**
	 * Returns the local color map representing each
	 * colored cell in this stand-alone multi-color puzzle.
	 */
	public Color[][] returnLocalColorMap()
	{
		return colorMap;
	}
	
}
